﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// Test_SetupDia.xaml 的交互逻辑
    /// </summary>
    public partial class Test_SetupDia : Window
    {
        public  int _number;
        /// <summary>
        /// 0. Means the recited words; 1. Means the un_recited words
        /// </summary>
        public int _type;
        public bool winner = false;
        private bool initFlag = false;

        public int SelectedListId;
        public string SelectedTag;
        private CourseWrapper _courseWrapper;

        public int ListCount;
        private List<string> tags;

        public Test_SetupDia()
        {
            InitializeComponent();
            _number = 5;
            _type = 0;
        }

        public Test_SetupDia(BackendModels.CourseWrapper _courseWrapper)
            : this()
        {
            this._courseWrapper = _courseWrapper;

            ListCount = ListRepository.GetListCount(_courseWrapper);
            tags = TagRepository.ListAllTags(_courseWrapper);

            for (int i = 1; i <= ListCount; i++)
            {
                comboBox2.Items.Add("List " + i);
            }
            for (int i = 0; i < tags.Count; i++)
                comboBox3.Items.Add(tags[i]);

            comboBox3.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;

            initFlag = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (IsValidnumber(this.textBox1.Text))
            {
                _type = this.comboBox1.SelectedIndex;
                _number = int.Parse(this.textBox1.Text);

                if (_type == 2)
                {
                    SelectedListId = comboBox2.SelectedIndex + 1;
                }
                else if (_type == 3)
                {
                    SelectedTag = tags[comboBox3.SelectedIndex];
                }

                winner = true; 
                this.Close();
            }
            else
                MessageBox.Show("数字非法");           

        }

        private bool IsValidnumber(string str)
        {
            bool flag = true;
            for (int i=0;i<str.Length;i++)
                if ((str[i] >= '0') && (str[i] <= '9')) { } else { flag = false; break; }
            if (flag)
            {
                int num = int.Parse(str);
                if ((num < 5) || (num > _courseWrapper.MaxTestSize)) { flag = false; }
            }
            return flag;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!initFlag) return;
            if (comboBox1.SelectedIndex == 2 && comboBox2 != null)
            {
                comboBox2.Visibility = Visibility.Visible;
            }
            else
            {
                comboBox2.Visibility = Visibility.Hidden;
            }

            if (comboBox1.SelectedIndex == 3 && comboBox3 != null)
            {
                comboBox3.Visibility = Visibility.Visible;
                if (tags.Count == 0)
                {
                    button1.IsEnabled = false;
                }
                else
                {
                    button1.IsEnabled = true;
                }
            }
            else
            {
                comboBox3.Visibility = Visibility.Hidden;
                button1.IsEnabled = true;
            }
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox2.Visibility == Visibility.Visible)
            {
                // list changed
                ListDataModel data = ListRepository.GetListData(this._courseWrapper, comboBox2.SelectedIndex + 1);
                textBox1.Text = data.ListCount.ToString();
            }
        }

        private void comboBox3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int wordCount = TagRepository.CountWordsOfTag(_courseWrapper, tags[comboBox3.SelectedIndex]);
            if (wordCount > _courseWrapper.MaxTestSize)
                wordCount = _courseWrapper.MaxTestSize;
            textBox1.Text = wordCount.ToString();
        }
    }
}
